// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class Saw extends cc.Component {

    @property({tooltip: "移动速度"})
    speed: number = 150;

    @property({tooltip: "移动距离"})
    distancesX: number = 0;

    @property({tooltip: "移动距离"})
    distancesY: number = 0;

    @property({tooltip: "开启移动"})
    move = true;

    initPos = cc.v2();
    endPos = cc.v2();
    time = 0;

    onLoad () {

    }

    start () {
        this.rotate();
        this.init();
    }

    init(){
        if(!this.move){
            return;
        }
        if(this.speed == 0){
            this.speed = 150;
        }
        this.node.getPosition(this.initPos);
        this.endPos = cc.v2(this.initPos.x + this.distancesX,this.initPos.y + this.distancesY);
        this.time = this.endPos.sub(this.initPos).mag() / this.speed;
        this.anim()
    }

    anim(){
        cc.tween(this.node)
            .repeatForever(
                cc.tween()
                    .to(this.time, {position: this.endPos})
                    .to(this.time, {position: this.initPos})
            )
            .start();
    }

    rotate(){
        cc.tween(this.node)
            .repeatForever(
                cc.tween()
                    .by(0.5, {angle: 360})
            )
            .start()
    }

    // update (dt) {}
}
